#include "../../../common/mysql_chat_session.hpp"
#include "../../../common/mysql_friend_apply.hpp"
#include "../../../common/mysql_relation.hpp"
#include <mysql/mysql.h>
#include <gflags/gflags.h>

#include "../../../odb/relation.hxx"
#include "../../../odb/friend_apply.hxx"
#include "../../../odb/chat_session.hxx"
#include "relation-odb.hxx"
#include "friend_apply-odb.hxx"
#include "chat_session-odb.hxx"
#include "chat_session_member-odb.hxx"

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");  //trace default 0

void rt_insert_test(xchat::RelationTable& relation_table)
{
   relation_table.insert("用户ID1", "用户ID2");
   relation_table.insert("用户ID1", "用户ID3");
}
void rt_friends_test(xchat::RelationTable& relation_table)
{
   auto res = relation_table.friends("用户ID1");
   for(auto i : res)
   {
      std::cout << i << std::endl;
   }
}
void rt_remove_test(xchat::RelationTable& relation_table)
{
   relation_table.remove("用户ID1", "用户ID2");
}
void rt_exists_test(xchat::RelationTable& relation_table)
{
   std::cout << relation_table.exists("用户ID1", "用户ID2") << std::endl;
   std::cout << relation_table.exists("用户ID3", "用户ID1") << std::endl;
   std::cout << relation_table.exists("用户ID1", "用户ID3") << std::endl;
   std::cout << relation_table.exists("用户ID3", "用户ID2") << std::endl;
}
void fat_insert_test(xchat::FriendApplyTable &friend_apply_table)
{
   xchat::FriendApply apply1("uuid1", "用户ID1", "用户ID2");
   friend_apply_table.insert(apply1);
   xchat::FriendApply apply2("uuid2", "用户ID1", "用户ID3");
   friend_apply_table.insert(apply2);
   xchat::FriendApply apply3("uuid3", "用户ID2", "用户ID3");
   friend_apply_table.insert(apply3);
}
void fat_remove_test(xchat::FriendApplyTable &friend_apply_table)
{
   friend_apply_table.remove("用户ID2", "用户ID3");
}
void fat_apply_users_test(xchat::FriendApplyTable &friend_apply_table)
{
   // xchat::FriendApply apply4("uuid4", "用户ID2", "用户ID3");
   // friend_apply_table.insert(apply4);
   auto res = friend_apply_table.apply_users("用户ID1");
   for(auto i : res)
   {
      std::cout << i << std::endl;
   }  
}
void fat_exists_test(xchat::FriendApplyTable &friend_apply_table)
{
   std::cout << friend_apply_table.exists("用户ID1", "用户ID2") << std::endl; //1 1 1 0
   std::cout << friend_apply_table.exists("用户ID1", "用户ID3") << std::endl;
   std::cout << friend_apply_table.exists("用户ID2", "用户ID3") << std::endl;
   std::cout << friend_apply_table.exists("用户ID3", "用户ID1") << std::endl;
}
void cst_insert_test(xchat::ChatSessionTable &chat_session_table)
{
   xchat::ChatSession ss1("会话ID1", "会话名称1", xchat::ChatSessionType::GROUP);
   chat_session_table.insert(ss1);
   xchat::ChatSession ss2("会话ID2", "会话名称2", xchat::ChatSessionType::SINGLE);
   chat_session_table.insert(ss2);
}
void cst_select_test(xchat::ChatSessionTable &chat_session_table)
{
   auto res = chat_session_table.select("会话ID1");
   std::cout << res->chat_session_id() << std::endl;
   std::cout << res->chat_session_name() << std::endl;
   std::cout << int(res->chat_session_type()) << std::endl;
}
void cst_singleChatSession_test(xchat::ChatSessionTable &chat_session_table)
{
   std::cout << "-----------cst_singleChatSession_test-----------" << std::endl;
   auto res = chat_session_table.singleChatSession("男");
   for(auto i : res)
   {
      std::cout << i._chat_session_id << " ";
      std::cout << i._friend_id << std::endl;
   }
}
void cst_groupChatSession_test(xchat::ChatSessionTable &chat_session_table)
{
   std::cout << "-----------cst_groupChatSession_test-----------" << std::endl;
   auto res = chat_session_table.groupChatSession("用户ID1");
   for(auto i : res)
   {
      std::cout << i._chat_session_id << " ";
      std::cout << i._chat_session_name << std::endl;
   }
}
void cst_remove_test(xchat::ChatSessionTable &chat_session_table)
{
   chat_session_table.remove("会话ID3");
}
void cst_remove_test2(xchat::ChatSessionTable &chat_session_table)
{
   chat_session_table.remove("男", "女");
}
int main(int argc, char* argv[])
{
   google::ParseCommandLineFlags(&argc, &argv, true);

   xchat::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

   auto db = xchat::ODBFactory::create("root", "299274142", "127.0.0.1", "xchat", "utf8", 0, 1);

   xchat::RelationTable rt(db);
   xchat::FriendApplyTable fat(db);
   xchat::ChatSessionTable cst(db);

   // rt_insert_test(rt);
   //rt_friends_test(rt);
   //rt_remove_test(rt);
   //rt_exists_test(rt);
   //fat_insert_test(fat);
   //fat_remove_test(fat);
   //fat_apply_users_test(fat);
   fat_exists_test(fat);
   //cst_insert_test(cst);
   //cst_select_test(cst);
   cst_singleChatSession_test(cst);
   // cst_groupChatSession_test(cst);
   // cst_remove_test(cst);
   // cst_singleChatSession_test(cst);
   cst_remove_test2(cst);
   cst_singleChatSession_test(cst);

   return 0;
}

 //odb -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time ../../../odb/relation.hxx